﻿using ReflectionStudio.Core.Events;
using System;
using System.Data;
using System.Data.OracleClient;
using System.Collections;
using System.Collections.Generic;
namespace WPFExcelReport
{
    public partial class VABPOS
    {
        public object CreateAB45(string ITXDBVERSION, DataSet db)
        {
            DataSet SReturn = new DataSet();
            DataSet SResult = new DataSet();
            DataTable tbNABPOS = new DataTable();
            DataTable tbABA = null;
            List<DataRow> list=null;
            string KRS = "", LFD = "", POS = "", ABPNEUBEN, ABPNEUDAT;
            string VKPKRS = "  ", VKPLFD = "000000", VKPTYE = " ", VKPPOS = "000";
            decimal ABPSOLLMENGE1 = 0, ABPSOLLMENGE2 = 0, ABPSOLLMENGE3 = 0, ABPSOLLMENGE4 = 0, ABPSOLLMENGE5 = 0, ABPSOLLMENGE6 = 0, ABPSOLLMENGE7 = 0,
      ABPSOLLMENGE8 = 0;
            string filter = "";
            DataRow[] rows;
            DataTable tbMU = db.Tables["ICC_ABAX"];
            DataTable tbABP = db.Tables["ABPOS_EXT"];
            int idx = 0;
            var qABP = from DataRow row in tbABP.AsEnumerable()
                       where row["ABPAUFART"].ToString() == "VW"
                       select row;
            foreach (DataRow dwABP in qABP)
            {
                list = new List<DataRow>();
                
                ABPNEUBEN = dwABP["ABPNEUBEN"].ToString();
                ABPNEUDAT = dwABP["ABPNEUDAT"].ToString();
                string ABPSTARTDAT = dwABP["ABPSTARTDAT"].ToString();
                string ABPTERMDAT = dwABP["ABPTERMDAT"].ToString();
                KRS = dwABP["ABPAUFKRS"].ToString();
                if (idx == 0)
                {
                    LFD = GetABP(ITXDBVERSION, KRS, ref db, dwABP);
                    list.Add(CreateABK(dwABP, ITXDBVERSION));
                }
                if (LFD == "" || LFD == "000000")
                {
                    throw new Exception("未能成功获取生产单号码:" + KRS);
                }
                idx++;
                POS = Helpers.Helper.Right("000" + dwABP["ABPAUFPOS"].ToString(), 3);
                ABPSOLLMENGE1 = Convert.ToDecimal(dwABP["ABPSOLLMENGE1"].ToString());
                ABPSOLLMENGE2 = Convert.ToDecimal(dwABP["ABPSOLLMENGE2"].ToString());
                ABPSOLLMENGE3 = Convert.ToDecimal(dwABP["ABPSOLLMENGE3"].ToString());
                ABPSOLLMENGE4 = Convert.ToDecimal(dwABP["ABPSOLLMENGE4"].ToString());
                ABPSOLLMENGE5 = Convert.ToDecimal(dwABP["ABPSOLLMENGE5"].ToString());
                ABPSOLLMENGE6 = Convert.ToDecimal(dwABP["ABPSOLLMENGE6"].ToString());
                ABPSOLLMENGE7 = Convert.ToDecimal(dwABP["ABPSOLLMENGE7"].ToString());
                ABPSOLLMENGE8 = Convert.ToDecimal(dwABP["ABPSOLLMENGE8"].ToString());
                #region=====ABPOS=========
                EventDispatcher.Instance.RaiseStatus("转换ABPOS" + KRS + LFD + POS, StatusEventType.RunningProgress);
                DataRow dwABPOS = INTEXDBC.Convert2ABPOS(dwABP, ITXDBVERSION);
                dwABPOS["ABPMANDANT"] = app.Mandant;
                dwABPOS["ABPAUFKRS"] = KRS;
                dwABPOS["ABPAUFLFD"] = LFD;
                dwABPOS["ABPAUFPOS"] = POS;
                dwABPOS["ABPDIVARTKZ"] = " ";
                dwABPOS["ABPAUFART"] = dwABP["ABPAUFART"];
                dwABPOS["ABPWAKZ"] = dwABP["ABPWAKZ"];
                dwABPOS["ABPART"] = dwABP["ABPART"];
                dwABPOS["ABPVAR"] = dwABP["ABPVAR"];
                dwABPOS["ABPFARBE"] = dwABP["ABPFARBE"];
                dwABPOS["ABPZLO"] = dwABP["ABPZLO"];
                dwABPOS["ABPZLB"] = dwABP["ABPZLB"];
                dwABPOS["ABPBLO"] = dwABP["ABPZLO"];

                dwABPOS["ABPVERSAEND"] = " ";
                dwABPOS["ABPEAKG"] = "0";
                dwABPOS["ABPSERIE"] = dwABP["ABPSERIE"];//轴号

                dwABPOS["ABPSME"] = dwABP["ABPSME"];

                dwABPOS["ABPSOLLMENGE1"] = dwABP["ABPSOLLMENGE1"];
                dwABPOS["ABPSOLLMENGE2"] = dwABP["ABPSOLLMENGE2"];
                dwABPOS["ABPSOLLMENGE3"] = dwABP["ABPSOLLMENGE3"];
                dwABPOS["ABPSOLLMENGE4"] = dwABP["ABPSOLLMENGE4"];
                dwABPOS["ABPSOLLMENGE5"] = dwABP["ABPSOLLMENGE5"];
                dwABPOS["ABPSOLLMENGE6"] = dwABP["ABPSOLLMENGE6"];
                dwABPOS["ABPSOLLMENGE7"] = dwABP["ABPSOLLMENGE7"];
                dwABPOS["ABPSOLLMENGE8"] = dwABP["ABPSOLLMENGE8"];

                dwABPOS["ABPZUGKG"] = 0;
                dwABPOS["ABPGESSTA"] = "TE";
                dwABPOS["ABPWBSTA"] = "60";
                dwABPOS["ABPWAART"] = "   ";
                dwABPOS["ABPMGV"] = 0;
                dwABPOS["ABPMGB"] = 0;
                dwABPOS["ABPSZUSATZKZ1"] = "	 ";
                dwABPOS["ABPSZUSATZKZ2"] = " ";
                dwABPOS["ABPKETTAUFT"] = " ";
                dwABPOS["ABPPOSTYP"] = "   ";
                dwABPOS["ABPFA"] = 0;
                dwABPOS["ABPGA"] = 0;
                dwABPOS["ABPQA"] = 0;
                dwABPOS["ABPAESTAT"] = " ";
                dwABPOS["ABPLIEFDAT"] = dwABP["ABPLIEFDAT"];
                dwABPOS["ABPVTDATE"] = dwABP["ABPVTDATE"];
                dwABPOS["ABPSTARTDAT"] = dwABP["ABPSTARTDAT"];
                dwABPOS["ABPENDDAT"] = dwABP["ABPENDDAT"];
                dwABPOS["ABPSTARTDATA"] = dwABP["ABPSTARTDATA"];
                dwABPOS["ABPENDDATA"] = dwABP["ABPENDDATA"];
                dwABPOS["ABPANRKR"] = dwABP["ABPANRKR"];
                dwABPOS["ABPALFDNR"] = dwABP["ABPALFDNR"];
                dwABPOS["ABPLIEFDATN"] = dwABP["ABPLIEFDATN"];
                dwABPOS["ABPPOSTYP"] = dwABP["ABPPOSTYP"];
                dwABPOS["ABPTERMDAT"] = dwABP["ABPTERMDAT"];
                dwABPOS["ABPSTARTDATN"] = dwABP["ABPSTARTDATN"];
                dwABPOS["ABPNEUBEN"] = dwABP["ABPNEUBEN"];
                dwABPOS["ABPNEUDAT"] = dwABP["ABPNEUDAT"];

                list.Add(dwABPOS);

                #endregion
                #region=====ABPR=========
                list.Add(CreateABR(dwABPOS, ITXDBVERSION));
                #endregion
                #region====ABAUFM=========
                EventDispatcher.Instance.RaiseStatus("转换ABAUFM" + KRS + LFD, StatusEventType.RunningProgress);
                DataRow dwProd = INTEXDBC.Convert2ABAUFM(null, ITXDBVERSION, false);
                dwProd["ABAMANDANT"] = app.Mandant; ;
                dwProd["ABAAUFTYP"] = "V";
                dwProd["ABAAUFKRS"] = KRS;
                dwProd["ABAAUFLFD"] = LFD;
                dwProd["ABAAUFPOS"] = POS;
                dwProd["ABAAPOS"] = "0001";
                dwProd["ABASGESLG"] = ABPSOLLMENGE1 * 10;
                dwProd["ABASGESGEW"] = ABPSOLLMENGE5 * 10;
                dwProd["ABASANZSTK"] = ABPSOLLMENGE8 / 100;
                dwProd["ABASSTKLG"] = Math.Round(ABPSOLLMENGE1 * ABPSOLLMENGE8) * 1000;
                dwProd["ABASSTKLGV"] = "100";
                dwProd["ABASSTKLGB"] = "99999000";
                dwProd["ABARSTSLG"] = 0;
                dwProd["ABARSTSGEW"] = 0;
                dwProd["ABALEAPOS"] = "2";
                dwProd["ABAZUPRIO"] = 9;
                dwProd["ABAASSCHNIT"] = "N";
                dwProd["ABAWOBST"] = " ";
                dwProd["ABAVAKZ"] = "J";
                dwProd["ABALIEFKOMP"] = " ";
                dwProd["ABANEUBEN"] = ABPNEUBEN;
                dwProd["ABANEUDAT"] = ABPNEUDAT;
                list.Add(dwProd);
                //Set4ITX.Merge(SResult);
                if (tbMU != null)
                {
                    filter = "ABAAUFKRS='" + KRS + "' AND ABAAUFLFD='" + LFD + "' AND ABAAUFPOS='" + POS + "'";
                    rows = tbMU.Select(filter);
                    foreach (DataRow dwReq in rows)
                    {
NewReservaton(dwProd, dwReq, ITXDBVERSION, "AB45MU01", ref list);
double Wnum = System.Math.Round(System.Convert.ToDouble(dwABP["ABPSOLLMENGE8"].ToString()) * 0.010, 0);
if (Wnum == 0)
    Wnum = 1;
dwReq["ABASANZSTK"] = Wnum;
double ABASSTKLG = System.Math.Round(System.Convert.ToDouble(dwABP["ABPSOLLMENGE1"].ToString()) * 10 / Wnum, 0);
dwReq["ABASSTKLG"] = ABASSTKLG;
dwReq["ABASSTKLGV"] = ABASSTKLG - 2000;
dwReq["ABASSTKLGB"] = ABASSTKLG + 2000;
                    }
                }
                #endregion

                #region=====ABFOLGE=========
                ConsoldateFlow(dwABPOS, ITXDBVERSION, "AB45", ref list);
                #endregion

                #region=====BAAUF===========

                DataRow dwBAAUF = INTEXDBC.Convert2BAAUF(dwABP, ITXDBVERSION, false);

                dwBAAUF["BAMANDANT"] = app.Mandant; ;
                dwBAAUF["BAAUFKRS"] = KRS;
                dwBAAUF["BAAUFLFD"] = LFD;
                dwBAAUF["BAAUFPOS"] = POS;
                dwBAAUF["BANEUBEN"] = ABPNEUBEN;
                dwBAAUF["BANEUDAT"] = ABPNEUDAT;
                dwBAAUF["BASA"] = "320";
                dwBAAUF["BAPOS"] = "0000";
                dwBAAUF["BAAFOLGE"] = "010";

                dwBAAUF["BADATVON"] = dwABP["ABPSTARTDAT"];
                dwBAAUF["BADATBIS"] = dwABP["ABPSTARTDAT"];
                dwBAAUF["BAKART"] = dwABP["ABPART"];
                dwBAAUF["BAKVAR"] = dwABP["ABPVAR"];
                dwBAAUF["BAKFARBE"] = dwABP["ABPFARBE"];
                dwBAAUF["BAMTR1"] = ABPSOLLMENGE1;
                dwBAAUF["BAANZ1"] = ABPSOLLMENGE8 / 100;
                dwBAAUF["BAMTR2"] = ABPSOLLMENGE1;
                dwBAAUF["BAANZ2"] = ABPSOLLMENGE8 / 100;
                dwBAAUF["BABAUMART"] = "      ";
                dwBAAUF["BADISPOKZ"] = " ";
                dwBAAUF["BALO"] = dwABP["ABPZLO"];
                dwBAAUF["BALB"] = dwABP["ABPZLB"];
                dwBAAUF["BALP"] = "01      ";
                dwBAAUF["BADOMKZ"] = " ";
                dwBAAUF["BAURKVAR"] = "  ";
                dwBAAUF["BABASMG"] = "0";

                list.Add(dwBAAUF);

                #endregion
                #region=====BABST====================
                decimal BAANZ1 = Convert.ToDecimal(dwABP["ABPSOLLMENGE8"].ToString()) / 100;
                decimal WLeng = Convert.ToDecimal(dwABP["ABPSOLLMENGE1"].ToString()) / 100;
                ABPSOLLMENGE5 = Convert.ToDecimal(dwABP["ABPSOLLMENGE5"].ToString()) / 100;
                decimal BMSOLLLG, BMSOLLGEW;
                BMSOLLLG = System.Math.Round(WLeng / BAANZ1, 0);
                BMSOLLGEW = System.Math.Round(ABPSOLLMENGE5 * 10 / Convert.ToDecimal(BAANZ1), 0);

                string BKRS = dwABP["ABPSERIE"].ToString();
                string BLFD = "";
                for (int bi = 0; bi < BAANZ1; bi++)
                {
                    DataRow dwBABST = INTEXDBC.Convert2BABST(null, ITXDBVERSION, false);
                    BLFD = VSDDAT.SD22.GETNEXT("0022", BKRS, "BABST");   
                    #region====JLTAB======
                    dwBABST["BMMANDANT"] = app.Mandant; ;
                    dwBABST["BMBAUMKRS"] = BKRS;
                    dwBABST["BMBAUMLFD"] = BLFD;
                    dwBABST["BMKART"] = dwABP["ABPART"];
                    dwBABST["BMKVAR"] = dwABP["ABPVAR"];
                    dwBABST["BMFARBE"] = dwABP["ABPFARBE"];
                    dwBABST["BMFDZ"] = dwABP["ABPAENDDAT"];//待定                   
                    dwBABST["BMKETTTYP"] = Helpers.Helper.Left(dwABP["ABPAENDDAT"].ToString(), 4);
                    dwBABST["BMKANTE"] = "  ";

                    dwBABST["BMSOLLLG"] = BMSOLLLG;
                    dwBABST["BMSOLLGEW"] = BMSOLLGEW;
                    dwBABST["BMSOLLVLG"] = "0"; 

                    dwBABST["BMAUFKRS"] = KRS;
                    dwBABST["BMAUFLFD"] = LFD;
                    dwBABST["BMAUFPOS"] = POS;
                    dwBABST["BMSA"] = "320";
                    dwBABST["BMAUFTYP"] = "V";

                    dwBABST["BMPRODLO"] = dwABP["ABPZLO"];
                    dwBABST["BMPRODLB"] = dwABP["ABPZLB"];
                    dwBABST["BMPRODLP"] = "01      ";
                    dwBABST["BMMNRKRS"] = "  ";
                    dwBABST["BMMLFDNR"] = "0320";
                    dwBABST["BMMPOS"] = "05";
                    dwBABST["BMMAPOS"] = "00";
                    dwBABST["BMNEUBEN"] = ABPNEUBEN;
                    dwBABST["BMNEUDAT"] = ABPNEUDAT;
                    #endregion
                    list.Add(dwBABST);
                }
                #endregion
                #region=====GAAUF===========
                filter = " ABAWAKZ='G' AND ABAVKAUFKRS='" + KRS + "' AND ABAVKAUFLFD='" + LFD + "' AND ABAVKAUFPOS='" + POS + "'";
                rows = tbMU.Select(filter);
                foreach (DataRow row in rows)
                {
                    DataRow dwGAAUF = INTEXDBC.Convert2GAAUF(null, ITXDBVERSION, false);
                    #region====JLT======
                    dwGAAUF["GAMANDANT"] = app.Mandant; ;
                    dwGAAUF["GAAUFKRS"] = KRS;
                    dwGAAUF["GAAUFLFD"] = LFD;
                    dwGAAUF["GAAUFPOS"] = POS;
                    dwGAAUF["GASA"] = "723";
                    dwGAAUF["GAPOS"] = "01";//待定
                    dwGAAUF["GANEUBEN"] = row["ABAUSER"];
                    dwGAAUF["GANEUDAT"] = row["ABADATE"];
                    dwGAAUF["GADATVON"] = dwABP["ABPSTARTDAT"];
                    dwGAAUF["GADATBIS"] = dwABP["ABPSTARTDAT"];
                    dwGAAUF["GAART"] = row["ABAART"];
                    dwGAAUF["GAVAR"] = row["ABAVAR"];
                    dwGAAUF["GAFARBE"] = row["ABAFARBE"];
                    dwGAAUF["GAKG1"] =Convert.ToDouble(row["ABARESMG"].ToString())/10;
                    dwGAAUF["GAKG2"] = Convert.ToDouble(row["ABARESMG"].ToString()) / 10;
                    dwGAAUF["GADISPOKZ"] = "V";
                    dwGAAUF["GAAUFTRKZ"] = "K";
                    dwGAAUF["GALO"] = row["ABALO"];
                    dwGAAUF["GALB"] = row["ABALB"];
                    dwGAAUF["GALP"] = row["ABALP"];
                    dwGAAUF["GAAUFBKZ"] = " ";
                    dwGAAUF["GABEM"] = "                ";
                    dwGAAUF["GABASMG2"] = 102000;
                    #endregion
                    list.Add(dwGAAUF);
                }

                #endregion
                #region===ICC_ABAX=============
                if (tbMU != null)
                {
                    /*
                    filter = "ABAAUFKRS='" + KRS + "' AND ABAAUFLFD='" + LFD + "' AND ABAAUFPOS='" + POS + "'";
                    rows = tbMU.Select(filter);
                    if (rows.Length > 0)
                    {
foreach (DataRow row in rows)
{
    DataRow dwABAX = INTEXDBC.Convert2ICC_ABAX(row, app.company, true);
    dwABAX["ABAID"] = 0;
    list.Add(dwABAX);
}
                    }*/
                }
                #endregion
                SaveAB(ITXDBVERSION, list);
            }
            return list;
        }
    }
}

